package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.page.PageBeen;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.TbBrandService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("brand")
public class TbBrandController {

    @Reference
    private TbBrandService tbBrandService;

    @RequestMapping("getTbBrand")
    @ResponseBody
    public Result getTbBrand(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "4")Integer pageSize){
        try {
            PageBeen<TbBrand> tbBrand = tbBrandService.getTbBrand(pageNum, pageSize);
            return Result.response(ResultEnum.SUCCESS).setData(tbBrand);
        }catch (Exception e){
            e.printStackTrace();
            PageBeen<TbBrand> tbBrand = tbBrandService.getTbBrand(pageNum, pageSize);
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }
    @RequestMapping("addBrand")
    @ResponseBody
    public Result addBrand(TbBrand tbBrand){
        try {
            Integer i = tbBrandService.addBrand(tbBrand);
           return Result.response(ResultEnum.SUCCESS);
        }catch (Exception e){
            Integer i = tbBrandService.addBrand(tbBrand);
           return Result.response(ResultEnum.ERROR);
        }

    }

    @RequestMapping("toUpdate/{id}")
    @ResponseBody
    public Result toUpdate(@PathVariable("id") Long id){
           TbBrand t = tbBrandService.toUpdate(id);
           return Result.response(ResultEnum.SUCCESS).setData(t);
    }
    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(TbBrand tbBrand){
           int t = tbBrandService.updateInfo(tbBrand);
           return Result.response(ResultEnum.SUCCESS);
    }
    @ResponseBody
    @RequestMapping("deleteInfo/{ids}")
    public Result deleteInfo(@PathVariable("ids")Integer[] ids){
        try {
            Integer i = tbBrandService.deleteInfo(ids);
            return Result.response(ResultEnum.SUCCESS);
        }catch (Exception e){
            Integer i = tbBrandService.deleteInfo(ids);
            return Result.response(ResultEnum.ERROR);
        }
    }


}
